package com.ingsing.tourism.controller;

import com.ingsing.common.enmus.ResultCode;
import com.ingsing.common.exception.MessageException;
import com.ingsing.common.resp.Base;
import com.ingsing.common.resp.Resp;
import com.ingsing.common.resp.UserCookie;
import com.ingsing.common.utils.Md5Utils;
import com.ingsing.tourism.entity.User;
import com.ingsing.tourism.service.Impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author houg
 * @create 2019/10/19 22:40
 * @explain
 */

@Controller
@RequestMapping(value = "/welcome")
public class LoginController {

    @Resource
    private UserServiceImpl userService;
    //用户登录
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    private Resp login(User user,HttpServletRequest request) {
        System.out.println("后台获取的对象："+user);
        System.out.println(user.getRemember());
        Resp resp = new Resp();
        if (null==user.getUsername()){
            new MessageException("请求错误！");
        }
        loginOption(user,resp,request);
        System.out.println(resp.getMsg());
        return resp;
    }

    private void loginOption(User user,Resp resp,HttpServletRequest request){
        User dbUser = null;
        String username = user.getUsername();
        String md5Password = Md5Utils.encrypt3Times(user.getPassword());
        //开始判断登录方式
        if (username.matches(Base.EMAIL_REGULAR)) {
            dbUser = userService.selectByEmail(username);
        }else if (username.matches(Base.PHONE_REGULAR)) {
            dbUser = userService.selectByPhone(username);
        }else {
            dbUser = userService.selectByUsername(username);
        }
        System.out.println("数据库查询结果："+dbUser);

        //判断用户是否存在
        if (null==dbUser){
            resp.setResultCode(ResultCode.ACCOUNT_NOT_EXIST);
            return;
        }
        //判断密码是否正确
        if (!md5Password.equals(dbUser.getPassword())){
            resp.setResultCode(ResultCode.LOGIN_PASSWORD_ERROR);
            System.out.println(user.getPassword()+dbUser.getPassword());
            System.out.println(md5Password);
            return;
        }
        switch (dbUser.getIsDelete()){
            case 1:
                HttpSession session = request.getSession();
                session.setAttribute(Base.ROLE_USER, dbUser);
                System.out.println("把用户信息存到Session:" + session.getId()+"用户是否记住密码"+user.getRemember());
                if (null != user.getRemember()){resp.setData(new UserCookie(session.getId(),dbUser.getUsername()));}
                //更新登录时间
                User nullUser = new User();
                nullUser.setLastLogin(new Date());
                int sums = userService.setLastLogin(nullUser);
                System.out.println(nullUser.getLastLogin());
                System.out.println("是否更新了登录时间："+sums);
                resp.setResultCode(ResultCode.LOGIN_SUCCESS);
                break;
            case 2:
                resp.setResultCode(ResultCode.ACCOUNT_OFF);
                break;
            case 0:
                resp.setResultCode(ResultCode.ACCOUNT_FORBIDDEN);
                break;
            default:
                resp.setResultCode(ResultCode.ACCOUNT_EXCEPTION);
                break;

        }
    }
}
